//
//  AddWorkExpViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "AddWorkExpViewController.h"
#import "UILabel+HWDUILabel.h"
#import "EditCell.h"
#import "SelfTextView.h"
#import "RWBChouseTimeView.h"

@interface AddWorkExpViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@property (nonatomic,strong)NSArray *titleArr;//标题
@property (nonatomic,strong)NSArray *BaseTitleArr;//标题

@property (nonatomic, copy) NSString *companyName;
@property (nonatomic, copy) NSString *place;
@property (nonatomic, copy) NSString *inTime;
@property (nonatomic, copy) NSString *outTime;
@property (nonatomic, copy) NSString *content;

@end

@implementation AddWorkExpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"添加工作经历"];
    [self initRightBarButtonWithTitle:@"保存" andTitleColor:[UIColor whiteColor] andFont:16*screenW_6];
    _BaseTitleArr = @[@"公司名称",@"你的职位",@"入职时间",@"离职时间"];
    if (_work) {
        _companyName = _work[@"companyName"];
        _place = _work[@"position"];
        _inTime = _work[@"entryTime"];
        _outTime = _work[@"turnoverTime"];
        _content = _work[@"jobContent"];
    }
    [self.view addSubview:self.tableV];
}
-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _BaseTitleArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 200*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditCell"];
    if (!cell) {
        cell = [[EditCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditCell"];
    }
    NSString *text;
    if (indexPath.row == 0) {
        text = _companyName;
    }else if (indexPath.row == 1){
        text = _place;
    }else if (indexPath.row == 2){
        text = _inTime;
    }else if (indexPath.row == 3){
        text = _outTime;
    }
    [cell loadCell:_BaseTitleArr[indexPath.row] text:text placeHodel:[NSString stringWithFormat:@"请输入%@",_BaseTitleArr[indexPath.row]]];
    __weak typeof(self) weakSelf = self;
    cell.blcok = ^(NSString *text){
        if (indexPath.row == 0) {
            weakSelf.companyName = text;
        }else if (indexPath.row == 1){
            weakSelf.place = text;
        }
    };
    if (indexPath.row == 2 || indexPath.row == 3) {
        cell.JianTouView.hidden = NO;
        cell.JianTouView.image = [UIImage imageNamed:@"data"];
        cell.textF.userInteractionEnabled = NO;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    [self.view endEditing:YES];
    if (indexPath.row == 2 || indexPath.row == 3) {
        RWBChouseTimeView *view = [[RWBChouseTimeView alloc]initWithDatePickerMode:UIDatePickerModeDate maxDate:nil minDate:nil Complete:^(NSDate *date) {
            if (indexPath.row == 2) {
                weakSelf.inTime = [TomamaUI dateTostiring:date andString:@"yyyy-MM-dd"];
            }else if (indexPath.row == 3){
                weakSelf.outTime = [TomamaUI dateTostiring:date andString:@"yyyy-MM-dd"];
            }
            [weakSelf.tableV reloadData];
        }];
        [view showOnView:self.view];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 190*screenW_6)];
    view.backgroundColor = [UIColor whiteColor];
    
    //标题
    UILabel *titleLab = [TomamaUI createLabel:CGRectMake(25*screenW_6,15*screenW_6,SCREENW-150*screenW_6,25*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:16*screenW_6] textColor:[UIColor colorWithHexString:@"#333333"] text:@"工作内容"];
    [view addSubview:titleLab];
    //输入框
    SelfTextView *inputTV = [[SelfTextView alloc]initWithFrame:CGRectMake(20*screenW_6, 50*screenW_6, SCREENW-40*screenW_6, 120*screenW_6) text:_content PlaceHodel:@"请输入详细工作内容～"];
    inputTV.backgroundColor = backColor;
    inputTV.TextV.backgroundColor = backColor;
    __weak typeof(self) weakSelf = self;
    inputTV.blcok = ^(NSString *text){
        weakSelf.content = text;
    };
    [view addSubview:inputTV];
    return view;
}


-(void)onTapRightBarButton{//保存
    if (_companyName.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请填写公司名称" timeInterval:2];
        return;
    }
    if (_place.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请填写职位" timeInterval:2];
        return;
    }
    if (_inTime.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请选择入职时间" timeInterval:2];
        return;
    }
    if (_outTime.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请选择离职时间" timeInterval:2];
        return;
    }
    if (_content.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请填写工作内容" timeInterval:2];
        return;
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"companyName":_companyName,@"position":_place,@"entryTime":_inTime,@"turnoverTime":_outTime,@"jobContent":_content}];
    NSString *url = @"/user/addWorkExperience";
    if (_work) {
        [dic setObject:_work[@"id"] forKey:@"id"];
        url = @"/user/updateWorkExperience";
    }else{
        [dic setObject:[Storage getToken] forKey:@"resumeId"];
    }
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:url Dic:dic completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [TomamaUI creatAlertWithTarget:self message:_work?@"修改成功":@"添加成功" timeInterval:2 block:^{
                        if (_block) {
                            _block(responseDic[@"data"]);
                        }
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}




@end
